package cn.iocoder.yudao.module.train.controller.admin.studentcourseperformance;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentCoursePerformanceStudentDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.studentcourseperformance.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.studentcourseperformance.StudentCoursePerformanceDO;
import cn.iocoder.yudao.module.train.service.studentcourseperformance.StudentCoursePerformanceService;

@Tag(name = "管理后台 - 学生课程作品")
@RestController
@RequestMapping("/train/student-course-performance")
@Validated
public class StudentCoursePerformanceController {

    @Resource
    private StudentCoursePerformanceService studentCoursePerformanceService;

    @PostMapping("/create")
    @Operation(summary = "创建学生课程作品")
    @PreAuthorize("@ss.hasPermission('train:student-course-performance:create')")
    public CommonResult<Integer> createStudentCoursePerformance(@Valid @RequestBody StudentCoursePerformanceSaveReqVO createReqVO) {
        return success(studentCoursePerformanceService.createStudentCoursePerformance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学生课程作品")
    @PreAuthorize("@ss.hasPermission('train:student-course-performance:update')")
    public CommonResult<Boolean> updateStudentCoursePerformance(@Valid @RequestBody StudentCoursePerformanceSaveReqVO updateReqVO) {
        studentCoursePerformanceService.updateStudentCoursePerformance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学生课程作品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:student-course-performance:delete')")
    public CommonResult<Boolean> deleteStudentCoursePerformance(@RequestParam("id") Integer id) {
        studentCoursePerformanceService.deleteStudentCoursePerformance(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除学生课程作品")
                @PreAuthorize("@ss.hasPermission('train:student-course-performance:delete')")
    public CommonResult<Boolean> deleteStudentCoursePerformanceList(@RequestParam("ids") List<Integer> ids) {
        studentCoursePerformanceService.deleteStudentCoursePerformanceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学生课程作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:student-course-performance:query')")
    public CommonResult<StudentCoursePerformanceRespVO> getStudentCoursePerformance(@RequestParam("id") Integer id) {
        StudentCoursePerformanceStudentDTO studentCoursePerformance = studentCoursePerformanceService.getStudentCoursePerformance(id);
        return success(BeanUtils.toBean(studentCoursePerformance, StudentCoursePerformanceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学生课程作品分页")
    @PreAuthorize("@ss.hasPermission('train:student-course-performance:query')")
    public CommonResult<PageResult<StudentCoursePerformanceRespVO>> getStudentCoursePerformancePage(@Valid StudentCoursePerformancePageReqVO pageReqVO) {
        PageResult<StudentCoursePerformanceStudentDTO> pageResult = studentCoursePerformanceService.getStudentCoursePerformancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StudentCoursePerformanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学生课程作品 Excel")
    @PreAuthorize("@ss.hasPermission('train:student-course-performance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStudentCoursePerformanceExcel(@Valid StudentCoursePerformancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudentCoursePerformanceStudentDTO> list = studentCoursePerformanceService.getStudentCoursePerformancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学生课程作品.xls", "数据", StudentCoursePerformanceRespVO.class,
                        BeanUtils.toBean(list, StudentCoursePerformanceRespVO.class));
    }

}
